<?php
/**
 * This file houses the Controller class.
 *
 * Copyright (c) 2010, Kalkomey Enterprises, Inc.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this 
 *   list of conditions and the following disclaimer in the documentation and/or other
 *   materials provided with the distribution.
 * - Neither the name of the Kalkomey Enterprises, Inc. nor the names of its 
 *   contributors may be used to endorse or promote products derived from this 
 *   software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @copyright Copyright (c) 2010, Kalkomey Enterprises, Inc.
 * @link http://code.google.com/p/swerv
 * @package Framework
 * @subpackage Library
 */

/**
 * The Controller class serves as the parent class to all controllers.
 *
 * @package Framework
 * @subpackage Library
 */
class Controller {
	
	/**
	 * A collection of components that can be used from inside the controller.
	 *
	 * @var array
	 */
	protected $components;

	/**
	 * All of the other non-explicitly-defined properties of this controller.
	 *
	 * @var array
	 */
	protected $properties;
	
	/**
	 * A protocol object used to fetch incoming data and format output.
	 *
	 * @var Protocol
	 */
	protected $protocol;
	
	/**
	 * An array of values passed via the URL
	 *
	 * @var array
	 */
	protected $urlParams;
	
	/**
	 * Object constructor.
	 *
	 * @param Protocol $protocol a Protocol object
	 * @param array $urlParams the URL parameters
	 *
	 * @return Controller
	 */
	public function __construct(&$protocol, $urlParams) {
		$this->components = array();
		$this->properties = array();
		$this->protocol = $protocol;
		$this->urlParams = $urlParams;
		if (method_exists($this, 'init')) {
			$this->init();
		}
	}
	
	/**
	 * Adds a component to this controller's collection.
	 *
	 * @param Component $obj
	 *
	 * @return void
	 */
	public function addComponent(Component $obj) {
		$componentName = get_class($obj);
		$this->components[$componentName] = $obj;
	}
	
	/**
	 * Overrides the PHP Magic method to get properties not explicitly defined.
	 *
	 * @param string $propertyName
	 *
	 * @return mixed
	 */
	public function __get($propertyName) {
		if (array_key_exists($propertyName, $this->components)) {
			return $this->components[$propertyName];
		} else if (array_key_exists($propertyName, $this->properties)) {
			return $this->properties[$propertyName];
		} else {
			return null;
		}
	}
	
	/**
	 * Sets output data in the protocol object so it will be formatted correctly.
	 *
	 * @uses Protocol::setData()
	 *
	 * @param string $name
	 * @param mixed $value
	 * 
	 * @return void
	 */
	public function setData($name, $value) {
		$this->protocol->setData($name, $value);
	}
	
	/**
	 * Overrides the PHP Magic method to set properties not explicitly defined.
	 *
	 * @param string $propertyName
	 * @param mixed  $value
	 *
	 * @return void
	 */
	public function __set($propertyName, $value) {
		$this->properties[$propertyName] = $value;
	}
	
	/**
	 * The main index for the controller.  This is used primarily by the internal documentation feature.
	 *
	 * @return void
	 */
	public function index() {
		
	}
	
}
